#include <iostream>

const int maxn = 1e4 + 10;
const int inf = 0x3f3f3f3f;
int n;
int mp[maxn][maxn]; // 邻接矩阵
int vis[maxn], dis[maxn]; // vis 哪些点在G集合, 哪些在T集合, dis G中与T可达的点距离

void prim() {
    vis[1] = 1;
    for (int i = 1; i <= n; i++) {
        dis[i] = mp[1][i];
    }
    for (int i = 2; i <= n; i++) {
        int minn = inf, v = -1;
        for (int j = 1; j <= n; j++) {
            if (vis[j] == 0 && dis[j] < minn) {
                minn = dis[j];
                v = j;
            }
        }
        vis[v] = 1;
        for (int j = 1; j <= n; j++) {
            if (vis[j] == 0 && dis[j] > mp[v][j]) {
                dis[j] = mp[v][j];
            }
        }
    }
    int sum = 0;
    for(int i = 1; i <= n; i++) {
        sum += dis[i];
    }
    printf("%d\n", sum);

    return ;
}

int main() {

    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            scanf("%d", &mp[i][j]);
        }
    }

    prim();

    return 0;
}